//
//  C_SongClassifyViewController.m
//  音悦咖
//
//  Created by Best on 15/7/15.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "C_SongClassifyViewController.h"
#import "C_SongButtonUICollectionViewCell.h"
#import "C_SongClassifyPlayerViewController.h"
#import "D_DataBase.h"

#define API_CLASSIFY_PLAYER_URL_FOUNT    @"http://api.dongting.com/channel/channel/"

#define API_CLASSIFY_PLAYER_URL_BLACK    @"/songs?size=50&page=1&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=07afc257564998d95c30d3961b852009acbd4bc7&idfa=344D8202-B8A1-4025-8A03-D1D21F9B1723&utdid=VZ9EyvXI9JADAKFnFQwx0zo5&alf=201200&bundle_id=com.ttpod.music"
@interface C_SongClassifyViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate, UICollectionViewDelegateFlowLayout>


@property (nonatomic, retain) NSMutableArray *titleArr;
@property (nonatomic, retain) NSMutableArray *imageArr;
@property (nonatomic, retain) NSArray *buttonArr;

@end

@implementation C_SongClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.rightButton.hidden = YES;
    self.view.backgroundColor = RGBA(50, 100, 30, 1);
    self.titleLabel.text = @"分类";
    blackBoubdImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    blackBoubdImageView.userInteractionEnabled = YES;
    
    blackBoubdImageView.image = [UIImage imageWithContentsOfFile:GETBUNDLE_PATH(@"blackgound", @"jpg")];
    [self.view addSubview:blackBoubdImageView];
    
    [self getdate];
    
}

#pragma mark getData
- (void)getdate
{
    [AFNetworkHandler GETWithAFNByURL:self.song_url completion:^(id result) {
        
        self.titleArr = [C_SongClassModel arrayWithModelByArray:[result objectForKey:@"data"]];
        [self getTitledata];
        NSLog(@"%ld", (unsigned long)self.buttonArr.count);
        [self createImageCollection];
        [self createButtonScroll];
        
    }];
}

#pragma mark 

- (void)getTitledata
{
    
      NSMutableSet *Mset = [NSMutableSet set];
    for (C_SongClassModel *model in self.titleArr) {
        
        [Mset addObject:model.parentname];
  
    }
    
    self.buttonArr = [Mset allObjects];
}

#pragma mark CreateButtonScroll
- (void)createButtonScroll
{
    buttonLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];

    buttonCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, imageCollection.frame.origin.y + 10, 90, imageCollection.frame.size.height - 10) collectionViewLayout:buttonLayout];
    buttonLayout.itemSize = CGSizeMake(40, 30);
//    buttonLayout.sectionInset = UIEdgeInsetsMake(40, 0, 40, 0);
    buttonLayout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
    buttonLayout.minimumLineSpacing = 40;
    buttonCollection.delegate = self;
    buttonCollection.dataSource = self;
    buttonCollection.tag = 3000;
    buttonLayout.collectionView.contentOffset = CGPointMake(0, (buttonLayout.itemSize.height + buttonLayout.minimumLineSpacing) * [buttonLayout.collectionView numberOfSections] / 2);
    
    buttonCollection.backgroundColor = [UIColor clearColor];
    [buttonCollection registerClass:[C_SongButtonUICollectionViewCell class] forCellWithReuseIdentifier:@"button"];
#pragma mark button 站位头
    [buttonCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"placeheader"];

    [blackBoubdImageView addSubview:buttonCollection];

}


#pragma mark createImagecollection

-(void)createImageCollection
{
    layout = [[[C_RedioUICollectionViewLayout alloc] init] autorelease];
    
    imageCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(90, 60, SCREEN_WIDTH - 100, SCREEN_HEIGHT - 64 - 200) collectionViewLayout:layout];
    layout.itemSize = CGSizeMake(imageCollection.frame.size.width - 40, 150);
    imageCollection.delegate = self;
    imageCollection.tag = 4000;
    imageCollection.dataSource = self;
    imageCollection.showsVerticalScrollIndicator = NO;
    imageCollection.backgroundColor = [UIColor clearColor];
    [blackBoubdImageView addSubview:imageCollection];
    [imageCollection release];
    layout.collectionView.contentOffset = CGPointMake(0, (layout.itemSize.height + layout.minimumLineSpacing) * [layout.collectionView numberOfSections] / 2);
    
    [imageCollection registerClass:[C_SongClassCollectionViewCell class] forCellWithReuseIdentifier:@"C_SongClassCollectionViewCell"];
    
}

#pragma mark header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 3000: {
            UICollectionReusableView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"placeheader" forIndexPath:indexPath];
            return reusable;
            break;
        }
        default:
            break;
    }
    
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    switch (collectionView.tag) {
        case 3000: {
            
            return CGSizeMake(collectionView.frame.size.width, 40);
        
            break;
        }
        default:
            break;
    }
    
    
    return CGSizeMake(0, 0);
}

#pragma mark collection Delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1000;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (collectionView.tag) {
        case 4000: {
            if (!self.imageArr) {
                return self.titleArr.count;
            } else {
                return self.imageArr.count;
            }
            break;
        }
        case 3000: {
            return self.buttonArr.count;
            break;
        }
        default:
            break;
    }
  
    return 0;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 4000: {
            C_SongClassCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_SongClassCollectionViewCell" forIndexPath:indexPath];
            if (!self.imageArr) {
                cell.model = self.titleArr[indexPath.item];
            } else {
                cell.model = self.imageArr[indexPath.item];
            
            }
            
            return cell;
            break;
        }
        case 3000: {
            C_SongButtonUICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"button" forIndexPath:indexPath];
            cell.buttonLabel.text = self.buttonArr[indexPath.item];
        
            cell.selectedBackgroundView.backgroundColor = COLOR_STOCHASTIC;
    
            cell.layer.cornerRadius = cell.frame.size.height / 2;
         
            [cell.buttonLabel setTextAlignment:(NSTextAlignmentCenter)];
            
        
            return cell;
            break;
        }
        default:
            break;
    }
    
    return nil;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 4000: {
            [blackBoubdImageView setImageWithURL:[self.imageArr[indexPath.item] pic_url_240_200]];
            C_SongClassifyPlayerViewController *player = [[C_SongClassifyPlayerViewController alloc] init];
            
            player.model = self.imageArr[indexPath.item];
            
            NSString *url = [NSString stringWithFormat:@"%@%@%@", API_CLASSIFY_PLAYER_URL_FOUNT, player.model.songlist_id, API_CLASSIFY_PLAYER_URL_BLACK];
            
            
            
            D_DataBase *data = [D_DataBase shareInstance];
//            [data openDB];
//            NSMutableArray *array = [data selectInforRecent];
//
//            for (C_RecentSkimModel *m in array) {
//                if ([m.title isEqual:player.model.songlist_name]) {
//                    NSLog(@"哈哈 有啦");
//                    return;
//                }
//            }
            C_RecentSkimModel *model = [[C_RecentSkimModel alloc] init];
            model.title = player.model.songlist_name;
            model.count = [player.model.quantity description];
            model.recent_url = url;
            
            [data insertInforRecentSkim:model];
            [data closeDB];
            
            
            [self.navigationController pushViewController:player animated:YES];
            
            break;
        }
        case 3000: {
#pragma mark 建立ImageArr
            self.imageArr = [NSMutableArray array];
                    for (C_SongClassModel *model in self.titleArr) {
                        if ([self.buttonArr[indexPath.item] isEqualToString:model.parentname]) {
                            [self.imageArr addObject:model];
                        }
                    }
                       [imageCollection reloadData];
            break;
        }
        default:
            break;
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
